package com.example.springbootwebfluxdemo.handler;

import com.example.springbootwebfluxdemo.mapper.HookMapper;
import com.example.springbootwebfluxdemo.pojo.Hook;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.concurrent.CompletableFuture;

import static org.springframework.web.reactive.function.BodyInserters.fromPublisher;

/**
 * @program: spring-webflux
 * @description: webflux的Handler写法，如果使用mybatis操作数据库，则此处可打开注释
 * @author: hewei
 * @date: 2021-11-05 11:27
 **/
@Slf4j
@Component
public class UserHandler {
//    @Autowired
//    private HookMapper hookMapper;
//
//    public Mono<ServerResponse> selectUserById(ServerRequest request){
//        Integer id=Integer.valueOf(request.queryParam("id").get());
//        Mono<Hook> user=Mono.fromFuture(CompletableFuture.supplyAsync(()->hookMapper.selectById(id)));
//        return user
//                .flatMap(u->
//                        ServerResponse.ok()
//                        .contentType(MediaType.APPLICATION_JSON)
//                        .body(fromPublisher(user,Hook.class))
//                )
//                .switchIfEmpty(ServerResponse.notFound().build());
//    }

//    public Mono<ServerResponse> selectUserByAddr(ServerRequest request)  {
//        String addr=request.queryParam("addr").get();
//        Future<List<User>> future= CompletableFuture.supplyAsync(()->userMapper.selectByAddr(addr));
//        List<User> users= null;
//        try {
//            users = future.get();
//        } catch (Exception e) {
//            log.error("UserHandler -> selectUserByAddr 查询异常：",e);
//        }
//        return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON)
//                .body(fromPublisher(Flux.fromIterable(users),User.class));
////        return Flux.fromStream(future.get().stream());
//
//    }
}
